package pos

import (
	"strings"
	"bytes"
	"bufio"
)

type PartOfSpeech byte

const ( 
	Noun PartOfSpeech = 'N'
	Plural PartOfSpeech = 'P'
	NounPhrase PartOfSpeech = 'h'
	Verb PartOfSpeech = 'V'
	VerbTransitive PartOfSpeech = 't'
	VerbIntransitive PartOfSpeech = 'i'
	Adjective PartOfSpeech = 'A'
	Adverb PartOfSpeech = 'v'
	Conjunction PartOfSpeech = 'C'
	Preposition PartOfSpeech = 'P'
	Interjection PartOfSpeech = '!'
	Pronoun PartOfSpeech = 'r'
	DefiniteArticle PartOfSpeech = 'D'
	IndefiniteArticle PartOfSpeech = 'I'
	Nominative PartOfSpeech = 'o'
)

type PartsOfSpeech []byte

type fact struct {
	w string
	p PartOfSpeech
}

var facts = make(map[fact]bool)
var definitions = make(map[string]PartsOfSpeech)

func parseParts(s string) PartsOfSpeech {

	return PartsOfSpeech(strings.Replace(s, "|", "", -1))
}

func init () {

	buf := bytes.NewBufferString(data)
	s := bufio.NewScanner(buf)

	for s.Scan() {


		line := s.Text()

		if line == "" {
			continue
		}

		tokens := strings.Split(line, "\t")
		w := tokens[0]
		ps := parseParts(tokens[1])

		definitions[w] = ps

		for _, p := range ps {
			facts[fact{w, PartOfSpeech(p)}] = true
		}
	}
}

func Is(word string, part PartOfSpeech) bool {

	return facts[fact{word, part}]
}

func Define(word string) PartsOfSpeech {

	return definitions[word]
}
